using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using C4F.DevKit.Bluetooth;
using C4F.DevKit.Bluetooth.SerialPortProfile;

namespace SerialPortProfileApplication
{

    public partial class PortSettings : Form
    {
        private int _baudRate;
        private int _dataBits;
        private Parity _parity;
        private byte _parityReplace;
        private StopBits _stopBits;
        private int _readBufferSize;
        private int _readTimeout;
        private int _writeBufferSize;
        private int _writeTimeout;
        private Encoding _encoding;
        private BluetoothSPPManager sppManager1;

        public PortSettings(BluetoothSPPManager sppManager)
        {
            InitializeComponent();
            sppManager1 = sppManager;

            cmbEncoding.Items.Add("ASCII");
            cmbEncoding.Items.Add("UTF7");
            cmbEncoding.Items.Add("UTF8");
            cmbEncoding.Items.Add("UTF32");

            cmbParity.Items.Add(Parity.Even);
            cmbParity.Items.Add(Parity.Mark);
            cmbParity.Items.Add(Parity.None);
            cmbParity.Items.Add(Parity.Odd);
            cmbParity.Items.Add(Parity.Space);

            cmbStopBits.Items.Add(StopBits.None);
            cmbStopBits.Items.Add(StopBits.One);
            cmbStopBits.Items.Add(StopBits.OnePointFive);
            cmbStopBits.Items.Add(StopBits.Two);

            txtBoxBaudRate.Text = sppManager1.BaudRate.ToString();
            txtBoxDataBits.Text = sppManager1.DataBits.ToString();
            txtBoxParityReplace.Text = sppManager1.ParityReplace.ToString();
            txtBoxReadBufSize.Text = sppManager1.ReadBufferSize.ToString();
            txtBoxReadTimeOut.Text = sppManager1.ReadTimeout.ToString();
            txtBoxWriteTimeOut.Text = sppManager1.WriteTimeout.ToString();
            txtWriteBufSize.Text = sppManager1.WriteBufferSize.ToString();

            if (sppManager1.Encoding is ASCIIEncoding)
                cmbEncoding.SelectedIndex = 0;
            if (sppManager1.Encoding is UTF32Encoding)
                cmbEncoding.SelectedIndex = 3;
            if (sppManager1.Encoding is UTF7Encoding)
                cmbEncoding.SelectedIndex = 1;
            if (sppManager1.Encoding is UTF8Encoding)
                cmbEncoding.SelectedIndex = 2;

            if (sppManager1.StopBits == StopBits.None)
                cmbStopBits.SelectedIndex = 0;
            if (sppManager1.StopBits == StopBits.One)
                cmbStopBits.SelectedIndex = 1;
            if (sppManager1.StopBits == StopBits.OnePointFive)
                cmbStopBits.SelectedIndex = 2;
            if (sppManager1.StopBits == StopBits.Two)
                cmbStopBits.SelectedIndex = 3;


            if (sppManager1.Parity == Parity.Even)
                cmbParity.SelectedIndex = 0;
            if (sppManager1.Parity == Parity.Mark)
                cmbParity.SelectedIndex = 1;
            if (sppManager1.Parity == Parity.None)
                cmbParity.SelectedIndex = 2;
            if (sppManager1.Parity == Parity.Odd)
                cmbParity.SelectedIndex = 3;
            if (sppManager1.Parity == Parity.Space)
                cmbParity.SelectedIndex = 3;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtBoxBaudRate.Text))
                {
                    MessageBox.Show("Please enter a valid value for Baud Rate.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBoxBaudRate.Focus();
                }
                if (string.IsNullOrEmpty(txtBoxDataBits.Text))
                {
                    MessageBox.Show("Please enter a valid value for Data Bits.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBoxDataBits.Focus();
                }
                if (cmbParity.SelectedIndex == -1)
                {
                    MessageBox.Show("Please enter a valid value for Parity.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbParity.Focus();
                }
                if (string.IsNullOrEmpty(txtBoxParityReplace.Text))
                {
                    MessageBox.Show("Please enter a valid value for Parity Replace.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBoxParityReplace.Focus();
                }
                if (string.IsNullOrEmpty(txtBoxReadBufSize.Text))
                {
                    MessageBox.Show("Please enter a valid value for Read Buffer Size.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBoxReadBufSize.Focus();
                }
                if (cmbStopBits.SelectedIndex == -1)
                {
                    MessageBox.Show("Please enter a valid value for Stop Bits.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbStopBits.Focus();
                }
                if (string.IsNullOrEmpty(txtBoxReadTimeOut.Text))
                {
                    MessageBox.Show("Please enter a valid value for Read TimeOut.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBoxReadTimeOut.Focus();
                }
                if (string.IsNullOrEmpty(txtBoxWriteTimeOut.Text))
                {
                    MessageBox.Show("Please enter a valid value for Write TimeOut", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBoxWriteTimeOut.Focus();
                }
                if (cmbEncoding.SelectedIndex == -1)
                {
                    MessageBox.Show("Please enter a valid value for Encoding.", "Port Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbEncoding.Focus();
                }
                else
                {
                    _baudRate = Convert.ToInt32(txtBoxBaudRate.Text);
                    sppManager1.BaudRate = _baudRate;
                    _dataBits = Convert.ToInt32(txtBoxDataBits.Text);
                    sppManager1.DataBits = _dataBits;
                    _parityReplace = Convert.ToByte(txtBoxParityReplace.Text);
                    sppManager1.ParityReplace = _parityReplace;
                    _readBufferSize = Convert.ToInt32(txtBoxReadBufSize.Text);
                    sppManager1.ReadBufferSize = _readBufferSize;
                    _readTimeout = Convert.ToInt32(txtBoxReadTimeOut.Text);
                    sppManager1.ReadTimeout = _readTimeout;
                    _writeBufferSize = Convert.ToInt32(txtWriteBufSize.Text);
                    sppManager1.WriteBufferSize = _writeBufferSize;
                    _writeTimeout = Convert.ToInt32(txtBoxWriteTimeOut.Text);
                    sppManager1.WriteTimeout = _writeTimeout;

                    switch (cmbEncoding.SelectedIndex)
                    {
                        case 0:
                            _encoding = new ASCIIEncoding();
                            break;
                        case 1:
                            _encoding = new UTF7Encoding();
                            break;
                        case 2:
                            _encoding = new UTF8Encoding();
                            break;
                        case 3:
                            _encoding = new UTF32Encoding();
                            break;
                    }
                    sppManager1.Encoding = _encoding;

                    switch (cmbParity.SelectedIndex)
                    {
                        case 0:
                            _parity = Parity.Even;
                            break;
                        case 1:
                            _parity = Parity.Mark;
                            break;
                        case 2:
                            _parity = Parity.None;
                            break;
                        case 3:
                            _parity = Parity.Odd;
                            break;
                        case 4:
                            _parity = Parity.Space;
                            break;
                    }
                    sppManager1.Parity = _parity;
                    switch (cmbStopBits.SelectedIndex)
                    {
                        case 0:
                            _stopBits = StopBits.None;
                            break;
                        case 1:
                            _stopBits = StopBits.One;
                            break;
                        case 2:
                            _stopBits = StopBits.OnePointFive;
                            break;
                        case 3:
                            _stopBits = StopBits.Two;
                            break;
                    }
                    sppManager1.StopBits = _stopBits;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Dispose();
            }
        }
    }
}